package com.szxy.optionalTest;

import jdk.nashorn.internal.runtime.options.Option;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @Author:zwer
 * @Date:2020/4/17 14:37
 * @Description:com.szxy.optionalTest
 * @Version:1.0
 **/
public class OptionalTest {

    @Test
    public void test1() {
        Optional<String> op = Optional.empty();
        System.out.println(op);
    }

    @Test
    public void test2() {
        Optional<String> op = Optional.of("HelloWorld");
        System.out.println(op);
    }

    /**
     * 如果 ofNullable 方法传入的参数是 null ，则返回一个 Optional.empty
     * 反之，返回一个 Optional 对象
     */
    @Test
    public void test3() {
        Optional<String> op2 = Optional.ofNullable("HelloJava");
        System.out.println("op = " + op2);

        Optional<String> op = Optional.ofNullable(null);
        System.out.println("op = " + op);
    }

    @Test
    public void test4() {
        String value = "Hello";
        Optional<String> op = Optional.of(value);
        String str = op.get();
        System.out.println("str = " + str);
    }

    @Test
    public void test5() {
        Optional<String> op = Optional.ofNullable(null);
        String value = op.orElse("DefaultValue");
        System.out.println("value = " + value);
    }

    @Test
    public void test6() {
        Optional<String> op = Optional.ofNullable(null);
        String str = op.orElseGet(() -> "Java");
        System.out.println(str);
    }

    @Test
    public void test7() {
        Optional<String> op = Optional.ofNullable(null);
        String str = op.orElseThrow(() -> new RuntimeException("用户名不合法"));
        System.out.println("str = " + str);
    }

    @Test
    public void test8() {
        Optional<String> op = Optional.of("Hello");
        // 过滤字符串 hello， 然后判断 op 对象是否有值
        boolean flag = op.filter(t -> !t.equals("Hello"))
                .isPresent();
        System.out.println("flag = " + flag);
        System.out.println("op = " + op);
    }

    @Test
    public void test9() {

    }


}
